//
//  JiaMiViewController.m
//  OCStudy
//
//  Created by tomato on 2021/8/23.
//

#import "JiaMiViewController.h"

#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCrypto.h>

#import "SSKeychain.h"

@interface JiaMiViewController ()

@end

@implementation JiaMiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
      [self base64Test];
//    [self aesTest];
//    [self MD5Test];
    
//    [self SSKeychainTest];
    
}
#pragma mark - 4、SSKeychain
-(void)SSKeychainTest{
    
   [SSKeychain setPassword:@"admin123" forService:[NSBundle mainBundle].bundleIdentifier account:@"张三"];
   
   NSString *mima = [SSKeychain passwordForService:[NSBundle mainBundle].bundleIdentifier account:@"张三"];
    
   NSLog(@"输出密码=%@",mima);
}

#pragma mark - 3、散列加密
-(void)MD5Test{
    
    NSString *str = @"我是中国人";
    NSString *str1 = [self md5EncryptWithString:str];
    NSLog(@"MD5加密=%@",str1);

}
//秘钥
static NSString *encryptionKey = @"nha735n197nxn(N′568GGS%d~~9naei';45vhhafdjkv]32rpks;lg,];:vjo(&**&^)";

- (NSString *)md5EncryptWithString:(NSString *)string{
    return [self md5:[NSString stringWithFormat:@"%@%@", encryptionKey, string]];
}

- (NSString *)md5:(NSString *)string{
    
    const char *cStr = [string UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), digest);
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [result appendFormat:@"%02X", digest[i]];
    }
    
    return result;
}

#pragma mark - 2、对称加密:加密、解密
-(void)aesTest{
    
    NSString *str = @"我是中国人";
    NSLog(@"原字符=%@",str);
    
    NSString *str1 = [self aesEncrypt:str] ;
    NSLog(@"aes加密后-%@",str1);
    
    NSString *str2 = [self aesDecrypt:str1];
    NSLog(@"aes解密后-%@",str2);
}

/*
 需要导入头文件
 #import <CommonCrypto/CommonDigest.h>
 #import <CommonCrypto/CommonCrypto.h>
*/
// 秘钥key
const NSString *AESKey = @"chsiahGHBh";

-(NSString *)aesEncrypt:(NSString *)sourceStr{
    
    if (!sourceStr){
        return nil;
    }
    
    char keyPtr[kCCKeySizeAES256];
    bzero(keyPtr, sizeof(keyPtr));
    [AESKey getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
     
    NSData *sourceData = [sourceStr dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [sourceData length];
    size_t buffersize = dataLength+kCCBlockSizeAES128;
    void *buffer = malloc(buffersize);
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode, keyPtr, kCCBlockSizeAES128, NULL, [sourceData bytes], dataLength, buffer, buffersize, &numBytesEncrypted);
     
    if (cryptStatus == kCCSuccess) {
        NSData *encryptData = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
        //对加密后的二进制数据进行base64转码
        return [encryptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
    } else {
        free(buffer);
        return nil;
    }
    
}

-(NSString *)aesDecrypt:(NSString *)secretStr{
    
   if (!secretStr) {
        return nil;
    }
    //先对加密的字符串进行base64解码
    NSData *decodeData = [[NSData alloc] initWithBase64EncodedString:secretStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
     
    char keyPtr[kCCKeySizeAES256 + 1];
    bzero(keyPtr, sizeof(keyPtr));
    [AESKey getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
     
    NSUInteger dataLength = [decodeData length];
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode, keyPtr, kCCBlockSizeAES128, NULL, [decodeData bytes], dataLength, buffer, bufferSize, &numBytesDecrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *data = [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
        NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        return result;
    } else {
        free(buffer);
        return nil;
    }
}


#pragma mark - 1、base64编码
-(void)base64Test{
    
    NSString *str = @"我是中国人";
    NSLog(@"原字符=%@",str);
    NSLog(@"base64编码=%@",[self base64BianMa:str]);
    NSLog(@"base64解码=%@",[self base64JieMa:[self base64BianMa:str]]);
}

-(NSString *)base64BianMa:(NSString *)str{
    
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64Str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return  base64Str;
}

-(NSString *)base64JieMa:(NSString *)str{
    
  NSData *data = [[NSData alloc]initWithBase64EncodedString:str options:NSDataBase64DecodingIgnoreUnknownCharacters];
  NSString *jiemaStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
   return  jiemaStr;
}

@end
